import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export const useHotelStore = defineStore('hotel', () => {
  // 状态
  const rooms = ref<any[]>([]);
  const tenants = ref<any[]>([]);
  const fees = ref<any[]>([]);
  const workOrders = ref<any[]>([]);
  const announcements = ref<any[]>([]);
  const loading = ref(false);

  // 统计数据
  const statistics = ref({
    totalRooms: 0,
    occupiedRooms: 0,
    vacantRooms: 0,
    totalTenants: 0,
    totalRevenue: 0,
    pendingWorkOrders: 0,
  });

  // 计算属性
  const roomOccupancyRate = computed(() => {
    if (statistics.value.totalRooms === 0) return 0;
    return (statistics.value.occupiedRooms / statistics.value.totalRooms) * 100;
  });

  const averageRevenue = computed(() => {
    if (statistics.value.totalRooms === 0) return 0;
    return statistics.value.totalRevenue / statistics.value.totalRooms;
  });

  // 方法
  const setRooms = (data: any[]) => {
    rooms.value = data;
  };

  const setTenants = (data: any[]) => {
    tenants.value = data;
  };

  const setFees = (data: any[]) => {
    fees.value = data;
  };

  const setWorkOrders = (data: any[]) => {
    workOrders.value = data;
  };

  const setAnnouncements = (data: any[]) => {
    announcements.value = data;
  };

  const setStatistics = (data: any) => {
    statistics.value = data;
  };

  const setLoading = (status: boolean) => {
    loading.value = status;
  };

  // 添加房间
  const addRoom = (room: any) => {
    rooms.value.push(room);
  };

  // 更新房间
  const updateRoom = (id: number, room: any) => {
    const index = rooms.value.findIndex((r) => r.id === id);
    if (index !== -1) {
      rooms.value[index] = { ...rooms.value[index], ...room };
    }
  };

  // 删除房间
  const deleteRoom = (id: number) => {
    const index = rooms.value.findIndex((r) => r.id === id);
    if (index !== -1) {
      rooms.value.splice(index, 1);
    }
  };

  return {
    // 状态
    rooms,
    tenants,
    fees,
    workOrders,
    announcements,
    loading,
    statistics,

    // 计算属性
    roomOccupancyRate,
    averageRevenue,

    // 方法
    setRooms,
    setTenants,
    setFees,
    setWorkOrders,
    setAnnouncements,
    setStatistics,
    setLoading,
    addRoom,
    updateRoom,
    deleteRoom,
  };
});
